package com.buildings.activity.reinforce;

import android.app.Activity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import com.buildings.R;
import com.buildings.bean.ImageURL;
import com.buildings.util.CustomToast;
import com.bumptech.glide.Glide;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shiyaolei
 * @packager com.buildings.activity.reinforce
 * @date 2020/8/27
 **/
public class ImageAdapter extends BaseAdapter {

  private List<ImageURL> urlList = new ArrayList<>();
  private Activity activity;
  private ImageAdapterListener listener;
  private int width;

  private List<String> serverList = new ArrayList<>();

  private int requestCode = 10001;

  private boolean isEdit=true;

  public void setEdit(boolean edit) {
    isEdit = edit;
  }

  public ImageAdapter(Activity activity, int W, int requestCode) {
    this.activity = activity;
    this.requestCode = requestCode;
    //add view
    ImageURL imageURL = new ImageURL();
    imageURL.setThumbnail("");
    urlList.add(imageURL);
    width = (int) (W * 0.26);
  }

  public void setUrlList(List<ImageURL> list) {

    urlList.clear();

    if (list != null && list.size() > 0) {
      urlList.addAll(list);
    }

    if (urlList.size() < 5) {
      ImageURL imageURL = new ImageURL();
      imageURL.setThumbnail("");
      urlList.add(imageURL);
    }

    notifyDataSetChanged();
  }

  @Override public int getCount() {
    return urlList == null ? 0 : urlList.size();
  }

  public List<ImageURL> getUrlList(){
    return urlList;
  }
  @Override public Object getItem(int position) {
    return urlList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    final ImageURL url = urlList.get(position);
    if (!TextUtils.isEmpty(url.getThumbnail())) {
      //4   8   1  8   1  8   4
      convertView = View.inflate(activity, R.layout.item_form_image, null);
      ImageView imageView = convertView.findViewById(R.id.picImageView);
      RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
      lp.width = width;
      lp.height = width;
      convertView.setLayoutParams(lp);
      ImageView deleteView = convertView.findViewById(R.id.closeImageView);
      if (isEdit){
        deleteView.setVisibility(View.VISIBLE);
      }else {
        deleteView.setVisibility(View.GONE);
      }
      Glide.with(activity).load(url.getThumbnail()).asBitmap().centerCrop().into(imageView);
      deleteView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
          if (listener != null) {
            listener.delete(position, url);
          }
          String url = urlList.get(position).getThumbnail();
          urlList.remove(position);
          serverList.remove(url);
          ImageURL imageURL = urlList.get(urlList.size()-1);
          if (!TextUtils.isEmpty(imageURL.getThumbnail())&&urlList.size()<5){
              urlList.add(new ImageURL());
          }
          notifyDataSetChanged();
        }
      });
    } else {
      convertView = View.inflate(activity, R.layout.item_form_image_add, null);
      if (!isEdit){
        convertView.setVisibility(View.GONE);
      }
      RelativeLayout layout = convertView.findViewById(R.id.picLayoutItem);
      RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width - 5, width - 5);
      lp.addRule(RelativeLayout.CENTER_IN_PARENT);
      convertView.setLayoutParams(lp);
      convertView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View view) {
          if (urlList.size() > 5) {
            CustomToast.showToast(activity, "最多选择5张图片");
            return;
          }
          if (listener != null) {
            listener.add(requestCode,urlList.size());
          }
        }
      });
    }
    return convertView;
  }

  public void update(List<ImageURL> list) {
    urlList.clear();
    if (list != null) {
      for (ImageURL url : list) {
        if (url != null) {
          urlList.add(url);
        }
      }
      if (list.size() != 5) {
        ImageURL imageURL = new ImageURL();
        imageURL.setThumbnail("");
        urlList.add(imageURL);
      }
    }
    notifyDataSetChanged();
  }


  public void setData(List<String> list) {
    urlList.clear();
    if (list != null) {
      for (String url : list) {
        if (!TextUtils.isEmpty(url)) {
          ImageURL u = new ImageURL();
          u.setThumbnail(url);
          u.setServerPath(url);
          urlList.add(u);
        }
      }
      if (list.size() != 5) {
        ImageURL imageURL = new ImageURL();
        imageURL.setThumbnail("");
        urlList.add(imageURL);
      }
    }
    serverList.addAll(list);
    notifyDataSetChanged();
  }

  public List<String> getStringList() {
    List<String> list = new ArrayList<>();
    if (urlList == null)
      return list;
    for (ImageURL url : urlList) {
      if (!TextUtils.isEmpty(url.getThumbnail())) {
        list.add(url.getThumbnail());
      }
    }
    return list;
  }

  public List<String> getServerList() {
    return serverList;
  }

  public void setServerList(List<String> serverList) {
    if (this.serverList.size()>0){
      if (serverList!=null){
        this.serverList.addAll(serverList);
      }
    }else {
      this.serverList.addAll(serverList);
    }
  }


  public void setListener(ImageAdapterListener listener) {
    this.listener = listener;
  }

  public interface ImageAdapterListener {
    void add(int requestCode,int size);

    void delete(int position, ImageURL url);
  }
}
